import {createRouter , createWebHashHistory} from 'vue-router'
import A from '../components/A.vue'
import B from '../components/B.vue'
import C from '../components/C.vue'


let router = createRouter({
    history:createWebHashHistory(),
    routes:[
        {
            path:"/",
            redirect:"/a"
        }
        ,
        {
            path:"/a",
            component:A
        },{
            path:"/b",
            component:B
        },
        {
            path:"/c",
            component:C
        }
    ]
});


//路由守卫的位置
//路由守卫就是监控组件的跳转 (进行拦截,跳转到其他的位置)
//前置路由守卫  preHandler()  检查 + 拦截 (进入组件之前)
/**
 * from : 你从哪里来 (哪个组件)
 * to   : 你要到哪里去 (到哪个组件)
 * next : 放行或者拦截
 */
router.beforeEach((from,to,next)=>{
    console.log("前置路由守卫")
    console.log("从哪来:"+from.path);
    console.log("到哪to:"+to.path);

    if(to.path == "/b"){
        next("/c"); 
    }else{
        //放行
        next();
    }
    
    //拦截 改变目标方向 next("/新的方向")
}); 
//后置路由守卫  postHandler()  后置记录 日志 (进入到组件了)
router.afterEach((from,to)=>{
    console.log("后置路由守卫")
    console.log("从哪来:"+from.path);
    console.log("到哪to:"+to.path);
});


export default router;